package com.cls.web;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.excel.EasyExcel;
import com.charles.utils.EasyExcelListener;
import com.cls.excel.AppTaskExcel;
import com.cls.excel.AppTaskImportResp;
import lombok.SneakyThrows;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author charles
 * @date 2024/8/14 10:38
 */
@RestController
public class ImportController {

    @SneakyThrows
    @RequestMapping("/import/t1")
    public List<AppTaskImportResp> t1(MultipartFile file) {
        EasyExcelListener<AppTaskExcel> easyExcelListener = new EasyExcelListener<>(AppTaskExcel.class);
        EasyExcel.read(file.getInputStream(), easyExcelListener)
                .sheet(0).head(AppTaskExcel.class)
                .doRead();
        easyExcelListener.getSuccessList().forEach(appTaskExcelDto -> {
            AppTaskExcel taskExcel = appTaskExcelDto.getT();

        });
        return easyExcelListener.getErrorList().stream().map(item -> {
                    AppTaskExcel t = item.getT();
                    AppTaskImportResp taskImportResp = BeanUtil.copyProperties(t, AppTaskImportResp.class);
                    taskImportResp.setErrMsg(item.getErrMsg());
                    taskImportResp.setLineNumber(item.getLineNumber());
                    return taskImportResp;
                }
        ).collect(Collectors.toList());
    }
}
